##################################################################
# File : /lua/ui/game/gamemain.lua
# Description : Game UI entry point
# Author : GPG / Neruz
##################################################################

local UIUtil = import('/lua/ui/uiutil.lua')
local LayoutHelpers = import('/lua/maui/layouthelpers.lua')
local EffectHelpers = import('/lua/maui/effecthelpers.lua')
local Group = import('/lua/maui/group.lua').Group
local WldUIProvider = import('/lua/ui/game/wlduiprovider.lua').WldUIProvider
local Bitmap = import('/lua/maui/bitmap.lua').Bitmap
local Movie = import('/lua/maui/movie.lua').Movie
local InGameUI = import('/lua/ui/game/InGameUI.lua')
local Text = import('/lua/maui/text.lua').Text

local isReplay = false

# check this flag to see if it's valid to show the exit dialog
supressExitDialog = false

function GetReplayState()
    return isReplay
end

PostScoreVideo = false

local provider = false

function CreateWldUIProvider()

    provider = WldUIProvider()

    provider.StartLoadingDialog = function(self, scenarioInfo)
        GetCursor():Hide()
    end

    provider.UpdateLoadingDialog = function(self, elapsedTime)
    end

    provider.StopLoadingDialog = function(self)
        GetCursor():Show()
        FlushEvents()
    end

    provider.StartWaitingDialog = function(self)
    end

    provider.UpdateWaitingDialog = function(self, elapsedTime)
    end

    provider.StopWaitingDialog = function(self)
        FlushEvents()
    end

    provider.CreateGameInterface = function(self, inIsReplay)
        isReplay = inIsReplay
        # Choose the game module to load based on the data set in front-end data
        # lobby or gameplay
        if GetFrontEndData('DSLobbySession' then
        	provider.gameInterface = import('/lua/ui/gamelobby/lobbyui.lua')
        else
        	provider.gameInterface = import('/lua/ui/game/gameui.lua')
        end
        provider.gameInterface.Create(isReplay)
        FlushEvents()
    end

    provider.DestroyGameInterface = function(self)
        provider.gameInterface.Destroy()
    end

    function provider:GetPrefetchTextures()
        return DiskFindFiles('/textures/ui/common/game/cursors', '*.dds')
    end
end

# This function is called whenever the set of currently selected units changes
# See /lua/modules/unit.lua for more information on the lua unit object
#      oldSelection: What the selection was before
#      newSelection: What the selection is now
#      added: Which units were added to the old selection
#      removed: Which units where removed from the old selection
# Callback that only happens when selection actually changes
SelectionChangedCallback = Callback()

# Callback for raw selection events (even if nothing changed)
SelectionCallback = Callback()

function OnSelectionChanged(oldSelection, newSelection, added, removed, ui_DebugSelect)
    if table.getn(added) > 0 or table.getn(removed) > 0 then
        SelectionChangedCallback:Call(oldSelection,newSelection,added,removed, ui_DebugSelect)
    else
        SelectionCallback:Call(oldSelection,newSelection,added,removed, ui_DebugSelect)
    end
end

QueueChangedCallback = Callback()
function OnQueueChanged(newQueue)
    QueueChangedCallback:Call(newQueue)
end

# Called immediately when the user hits the pause button. This only ever gets
# called on the machine that initiated the pause (i.e. other network players
# won't call this)
function OnUserPause(pause)
    local likelyTimeouts = import('/lua/ui/game/MODULE_pause.lua').GetLikelyTimeouts()
    local focus = GetArmiesTable().focusArmy
    if likelyTimeouts != 0 then
        if focus == -1 and not SessionIsReplay() then
            return
        end
        PauseSound("World",pause)
        PauseSound("Music",pause)
        PauseVoice("VO",pause)

        if SessionIsReplay() then
            import('/lua/ui/game/options.lua').SetPauseButton(pause,true)
        end
    end
end

BeatCallback = Callback()

# this function is called whenever the sim beats
function OnBeat()
    BeatCallback:Call()
end

FrameCallback = Callback()
function OnFrame()
    FrameCallback:Call()
end

function HideGameUI(movieBool)
    WARN('HideGameUI not yet implemented.')
end

# Given a userunit that is adjacent to a given blueprint, does it yield a
# bonus? Used by the UI to draw extra info
function OnDetectAdjacencyBonus(userUnit, otherBp)
    # fixme: todo
    return true
end


function OnFocusArmyUnitDamaged(unit)
    import('/lua/UserMusic.lua').NotifyBattle()
end

UnitCreatedCallback = Callback()
function OnUnitCreated(unit)
    UnitCreatedCallback:Call(unit)
end

UnitDestroyedCallback = Callback()
function OnUnitDestroyed(unit)
    UnitDestroyedCallback:Call(unit)
end

function ToggleTeamColorMode()
    teamColorMode = teamColorMode + 1
    if teamColorMode > 1 then
        teamColorMode = 0
    end

    SetArmyColorMode(teamColorMode)
end